package com.huawei.zyb.taskManager.domain.project.entity;

import com.huawei.zyb.taskManager.domain.project.valueobject.*;
import com.huawei.zyb.taskManager.domain.developer.valueobject.DeveloperId;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 项目聚合根
 * 管理项目信息和生命周期
 */
public class Project {
    private ProjectId id;
    private ProjectName name;
    private ProjectDescription description;
    private ProjectStatus status;
    private DeveloperId managerId; // 项目经理ID
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    // 私有构造函数，强制使用工厂方法
    private Project() {}

    /**
     * 创建新项目
     */
    public static Project create(
            ProjectId id,
            ProjectName name,
            ProjectDescription description,
            DeveloperId managerId) {
        
        Project project = new Project();
        project.id = id;
        project.name = name;
        project.description = description;
        project.status = ProjectStatus.PLANNING;
        project.managerId = managerId; // 支持创建时分配项目经理
        project.createdAt = LocalDateTime.now();
        project.updatedAt = LocalDateTime.now();
        
        return project;
    }
    
    /**
     * 从持久化数据恢复项目对象
     */
    public static Project restore(
            ProjectId id,
            ProjectName name,
            ProjectDescription description,
            ProjectStatus status,
            DeveloperId managerId,
            LocalDateTime createdAt,
            LocalDateTime updatedAt) {
        
        Project project = new Project();
        project.id = id;
        project.name = name;
        project.description = description;
        project.status = status;
        project.managerId = managerId;
        project.createdAt = createdAt;
        project.updatedAt = updatedAt;
        
        return project;
    }

    /**
     * 更新项目状态
     */
    public void updateStatus(ProjectStatus newStatus) {
        if (!this.status.canTransitionTo(newStatus)) {
            throw new IllegalStateException(
                String.format("项目状态不能从[%s]转换到[%s]", 
                    this.status.getDescription(), newStatus.getDescription())
            );
        }
        
        this.status = newStatus;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 更新项目信息
     */
    public void updateInfo(
            ProjectName name, 
            ProjectDescription description) {
        
        if (this.status.isFinalState()) {
            throw new IllegalStateException("已完成或已取消的项目不能修改信息");
        }
        
        this.name = Objects.requireNonNull(name, "项目名称不能为空");
        this.description = Objects.requireNonNull(description, "项目描述不能为空");
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 分配项目经理
     */
    public void assignManager(DeveloperId managerId) {
        if (this.status.isFinalState()) {
            throw new IllegalStateException("已完成或已取消的项目不能重新分配项目经理");
        }
        
        this.managerId = Objects.requireNonNull(managerId, "项目经理不能为空");
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 启动项目（从规划状态转为进行中状态）
     */
    public void start() {
        if (this.managerId == null) {
            throw new IllegalStateException("项目必须分配项目经理后才能启动");
        }
        updateStatus(ProjectStatus.ACTIVE);
    }

    /**
     * 暂停项目
     */
    public void suspend() {
        updateStatus(ProjectStatus.SUSPENDED);
    }

    /**
     * 恢复项目（从暂停状态转为进行中状态）
     */
    public void resume() {
        if (this.status != ProjectStatus.SUSPENDED) {
            throw new IllegalStateException("只有暂停状态的项目才能恢复");
        }
        updateStatus(ProjectStatus.ACTIVE);
    }

    /**
     * 完成项目
     */
    public void complete() {
        updateStatus(ProjectStatus.COMPLETED);
    }

    /**
     * 取消项目
     */
    public void cancel() {
        updateStatus(ProjectStatus.CANCELLED);
    }

    /**
     * 检查是否可以创建需求
     */
    public boolean canCreateRequirements() {
        return this.status.canCreateRequirements();
    }

    /**
     * 检查是否已完成
     */
    public boolean isCompleted() {
        return this.status == ProjectStatus.COMPLETED;
    }

    /**
     * 检查是否为终态
     */
    public boolean isFinalState() {
        return this.status.isFinalState();
    }

    /**
     * 检查是否为活跃状态
     */
    public boolean isActive() {
        return this.status.isActive();
    }



    // Getters
    public ProjectId getId() { return id; }
    public ProjectName getName() { return name; }
    public ProjectDescription getDescription() { return description; }
    public ProjectStatus getStatus() { return status; }
    public DeveloperId getManagerId() { return managerId; }
    public LocalDateTime getCreatedAt() { return createdAt; }
    public LocalDateTime getUpdatedAt() { return updatedAt; }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Project project = (Project) obj;
        return Objects.equals(id, project.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Project{" +
                "id=" + id +
                ", name=" + name +
                ", status=" + status +
                ", managerId=" + managerId +
                "}";
    }
}